package cn.blogss.tutorial;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author: Thatcher Li
 * @Date: 2021/3/19
 * @Descripttion: Iterates though all people in the AddressBook and prints info from them.
 */
public class ListPerson {
    public static void main(String[] args) throws IOException {
        if(args.length != 1){
            System.err.print("Usage: ListPeople ADDRESS_BOOK_FILE");
            System.exit(-1);
        }

        // Read the existing address book.
        AddressBookProto.AddressBook addressBook = AddressBookProto.AddressBook.parseFrom(new FileInputStream(args[0]));

        print(addressBook);
    }

    private static void print(AddressBookProto.AddressBook addressBook) {
        for (AddressBookProto.Person person: addressBook.getPeopleList()){
            System.out.println("Person ID: " + person.getId());
            System.out.println("  Name: " + person.getName());
            //if(person.hasEmail()){ // optional 关键字修饰的字段会生成 hasFieldName()方法用来判断消息体中是否有这个字段。
                System.out.println("  E-mail address: " + person.getEmail());
            //}

            for (AddressBookProto.Person.PhoneNumber phoneNumber: person.getPhonesList()){
                switch (phoneNumber.getType()) {
                    case MOBILE:
                        System.out.print("  Mobile phone #: ");
                        break;
                    case HOME:
                        System.out.print("  Home phone #: ");
                        break;
                    case WORK:
                        System.out.print("  Work phone #: ");
                        break;
                }
                System.out.println(phoneNumber.getNumber());
            }
        }
    }
}
